#include "stdafx.h"
#include "ChatHitogramView.h"



namespace DuiLib
{
	IMPLEMENT_DUICONTROL(CChatHitogramViewUI)

	CChatHitogramViewUI::CChatHitogramViewUI()
	{
	}

	CChatHitogramViewUI::~CChatHitogramViewUI()
	{
	}

	void CChatHitogramViewUI::setModel(CAbstractItemModel* pModel)
	{
		CAbstractItemView::setModel(pModel);

		showModel();
	}

	void CChatHitogramViewUI::onDataChanged(const CModelIndex& index,
		const CVar& oldValue, const CVar& newValue)
	{
		CAbstractItemModel* pModel = model();
		if (!pModel)
		{
			return;
		}

		int row = index.row();
		CModelIndex&& indexName = model()->index(row, 1);
		CModelIndex&& indexCount = model()->index(row, 2);
		CDuiString name = pModel->data(indexName).WideStrVal();
		int count = pModel->data(indexCount).IntVal();
		if (name.IsEmpty())
		{
			return;
		}

		this->SetCount(name, count);
	}

	void CChatHitogramViewUI::showModel()
	{
		CAbstractItemModel* pModel = model();
		if (!pModel)
		{
			return;
		}

		for (int row = 0; row < pModel->rowCount(); row++)
		{
			CModelIndex&& indexName = pModel->index(row, 1);
			CModelIndex&& indexCount = pModel->index(row, 2);

			CDuiString name = pModel->data(indexName).WideStrVal();
			int count = pModel->data(indexCount).IntVal();

			this->SetCount(name, count);
		}
	}

}